package cn.rainshare.io;

import cn.rainshare.dao.StuDao;
import cn.rainshare.entity.Student;
import cn.rainshare.service.StuService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

public class MyTest {


    public void Test01() {
        String config = "applicationContext.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        String[] names = ctx.getBeanDefinitionNames();

        for (String name : names) {

            System.out.println("容器中的对象="+ name + "|" + ctx.getBean(name).getClass().getName());
        }
    }


    public void Test02() {
        String config = "applicationContext.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        StuDao stuDao = (StuDao)ctx.getBean("stuDao");


        int res = stuDao.insertStu(new Student(1001,"张三"));

        System.out.println(res);
    }


    public void Test03() {
        String config = "applicationContext.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        StuService stuService = (StuService)ctx.getBean("stuService");

        int res = stuService.addStu(new Student(1002,"李四"));

        System.out.println(res);
    }


    public void Test04() {
        String config = "applicationContext.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        StuService stuService = (StuService)ctx.getBean("stuService");

        List<Student> res = stuService.queryAllStu();

        for (Student student : res){
            System.out.println(student);
        }

    }


    public void Test05() {
        String config = "applicationContext.xml";

        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        StuService stuService = (StuService)ctx.getBean("stuService");

        int res = stuService.addStu(new Student(999,"王五"));

        System.out.println(res);
    }



    public void cp() {
        File in = new File("E:\\Chia\\plot-k32-2021-05-02-19-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049fce82c51ef9.plot");
        File out = new File("C:\\Chia\\plot-k32-2021-05-02-19-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049fce82f51ef9.plot");
        fileCopyWithFileChannel(in,out);
    }

    /**
     * 用filechannel进行文件复制
     *
     * @param fromFile 源文件
     * @param toFile   目标文件
     */
    public void fileCopyWithFileChannel(File fromFile, File toFile) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        FileChannel fileChannelInput = null;
        FileChannel fileChannelOutput = null;
        try {
            fileInputStream = new FileInputStream(fromFile);
            fileOutputStream = new FileOutputStream(toFile);
            //得到fileInputStream的文件通道
            fileChannelInput = fileInputStream.getChannel();
            //得到fileOutputStream的文件通道
            fileChannelOutput = fileOutputStream.getChannel();
            //将fileChannelInput通道的数据，写入到fileChannelOutput通道
            fileChannelInput.transferTo(0, fileChannelInput.size(), fileChannelOutput);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
                if (fileChannelInput != null)
                    fileChannelInput.close();
                if (fileOutputStream != null)
                    fileOutputStream.close();
                if (fileChannelOutput != null)
                    fileChannelOutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }






    public void Test() throws IOException {
        File out = new File("C:\\Chia\\plot-k32-2021-05-02-19-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049fce82f51ef9.plot");


        MappedBiggerFileReader reader = new MappedBiggerFileReader("E:\\Chia\\plot-k32-2021-05-02-19-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049fce82c51ef9.plot", 100);
        long start = System.nanoTime();
        System.out.println("FileLength=" + reader.getFileLength());

        while (reader.read() != -1) {
            //if (count++ == 10) break;


        }


        long end = System.nanoTime();
        reader.close();
        System.out.println("MappedBiggerFileReader: " + (end - start));
    }


    public void Test08() throws IOException {
        // 定义源文件 & 目标文件
        String infile = "E:\\Chia\\plot-k32-2021-05-02-19-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049fce82c51ef9.plot";
        String outfile = "C:\\Chia\\plot-k32-2021-05-03-16-16-b09ca14201712e8c9a5d823ec8f3a03c3ab51d029418cba0c0049ffe82c51ef9.plot";

        // 1. 获取数据源 和 目标传输地的输入输出流
        FileInputStream fin = new FileInputStream(infile);
        FileOutputStream fout = new FileOutputStream(outfile);

        // 2. 获取数据源的输入输出通道
        FileChannel fcin = fin.getChannel();
        FileChannel fcout = fout.getChannel();

        // 3. 创建缓冲区对象，容量初始化为1024
        ByteBuffer buff = ByteBuffer.allocate(1024 * 1024 * 10);

        while (true) {
            // 4. 从通道读取数据 & 写入到缓冲区
            int r = fcin.read(buff);
            // 返回-1代表已读取到该通道数据的末尾，循环可以结束
            if (r == -1) {
                break;
            }
            // 5. 传出数据准备：调用flip()方法
            buff.flip();

            // 6. 从 Buffer 中读取数据 & 传出数据到目标的输入通道
            fcout.write(buff);
            // 7. 复位缓冲区
            buff.clear();
        }
    }


}
